//
//  TopicViewController.m
//  Findif
//
//  Created by Mac on 2019/2/22.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "TopicViewController.h"
#import "TopicTableViewCell.h"
#import "TopicHeaderView.h"
#import "PushTopicViewController.h"
#import "TopicModel.h"
#import "TopicDetailViewController.h"

@interface TopicViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UIButton * rightButton;
@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <TopicModel *>* todayArray;
@property (nonatomic, strong)NSArray <TopicModel *>* historyArray;
@property (nonatomic, weak)UIImageView * spaceImageView;
@property (nonatomic, weak)UILabel * spaceLabel;

@end

@implementation TopicViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [[ZRNetworkTool sharedTool] getTopicListWithPromulgator_id:self.group_id WithBlock:^(BOOL success, id responseData) {
        
//        NSLog(@"res==%@",responseData);
        if (success) {
            
            NSDictionary * dict = responseData[@"data"];
            
            self.historyArray = [NSArray yy_modelArrayWithClass:[TopicModel class] json:dict[@"history"]];
            self.todayArray = [NSArray yy_modelArrayWithClass:[TopicModel class] json:dict[@"today"]];
            
        }
    }];
}


-(void)setTodayArray:(NSArray<TopicModel *> *)todayArray{
    
    _todayArray = todayArray;
    
    [self.tableView reloadData];
}


- (void)setHistoryArray:(NSArray<TopicModel *> *)historyArray{
    
    _historyArray = historyArray;
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupNav];
    [self setupUI];
}


- (void)setupNav{
    
    self.title = @"话题";
    
    UIButton * rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 15 * SCREENH6)];
    
    [rightButton setTitle:@"创建" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didSetUpButton) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.rightButton = rightButton;
    
    UIBarButtonItem * rightBar = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    
    self.navigationItem.rightBarButtonItem = rightBar;
    
    if ([self.identity isEqualToString:@"1"]) {
        
        self.rightButton.hidden = NO;
    }else{
        
        self.rightButton.hidden = YES;
    }
}

- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF4F5F6);
    tableView.rowHeight = 80 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
//    TableViewHeader * headerView =  [[TableViewHeader alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 270 * SCREENH6)];
//    // 设置header
//    tableView.tableHeaderView = headerView;
    //    self.tableView.bounces = NO;
    //    self.tableView.estimatedRowHeight = 150 * SCREENH6;//估算高度
    //    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[TopicTableViewCell class] forCellReuseIdentifier:@"TopicTableViewCell"];
    [self.tableView registerClass:[TopicHeaderView class] forHeaderFooterViewReuseIdentifier:@"TopicHeaderView"];
    
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//
//        [self loadData];
//    }];
    
    [self.view addSubview:self.tableView];
    
    
    UIImageView * spaceImageView = [UIImageView new];
    
    spaceImageView.image = [UIImage imageNamed:@"状态为空"];
    spaceImageView.hidden = YES;
    
    self.spaceImageView = spaceImageView;
    
    [self.tableView addSubview:self.spaceImageView];
    
    
    UILabel * spaceLabel = [UILabel new];
    
    spaceLabel.text = @"社团暂无话题";
    spaceLabel.textColor = [UIColor lightGrayColor];
    spaceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    spaceLabel.textAlignment = NSTextAlignmentCenter;
    spaceLabel.hidden = YES;
    
    self.spaceLabel = spaceLabel;
    
    [self.tableView addSubview:self.spaceLabel];
}


- (void)viewDidLayoutSubviews{
    
    [self.spaceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.tableView);
        make.width.mas_equalTo(169 * SCREENH6);
        make.height.mas_equalTo(136 * SCREENH6);
    }];
    
    [self.spaceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.spaceImageView);
        make.top.mas_equalTo(self.spaceImageView.mas_bottom).offset(10 * SCREENH6);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.todayArray.count > 0 && self.historyArray.count > 0) {

        self.spaceLabel.hidden = YES;
        self.spaceImageView.hidden = YES;
        return 2;
    }else if (self.todayArray.count > 0 || self.historyArray.count > 0){

        self.spaceLabel.hidden = YES;
        self.spaceImageView.hidden = YES;
        
        return 1;
    }else{

        self.spaceLabel.hidden = NO;
        self.spaceImageView.hidden = NO;
        return 0;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0) {
        
        if (self.todayArray.count > 0) {
            
            return self.todayArray.count;
        }else{
            
            return self.historyArray.count;
        }
    }else{
        
        return self.historyArray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        if (self.todayArray.count > 0) {
            
            TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicTableViewCell" forIndexPath:indexPath];
            
            cell.model = self.todayArray[indexPath.row];
            
            return cell;
        }else{
            
            TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicTableViewCell" forIndexPath:indexPath];
            
            cell.model = self.historyArray[indexPath.row];
            
            return cell;
        }
        
    }else{
        
        TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicTableViewCell" forIndexPath:indexPath];
        
        cell.model = self.historyArray[indexPath.row];
        
        return cell;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (self.todayArray.count > 0 && self.historyArray.count > 0) {
        
        return 30 * SCREENH6;
    }else if(self.todayArray.count > 0 || self.historyArray.count > 0){
        
        if (section == 0) {
            
            return 30 * SCREENH6;
        }else{
            
            return 0;
        }
    }else{
        
        return 0;
    }
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    TopicHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"TopicHeaderView"];
    
    if (section == 0) {
        
        if (self.todayArray.count > 0) {
            
            header.titleLabel.text = @"今日话题";
        }else{
            
            header.titleLabel.text = @"历史话题";
        }
        
    }else{
        
       header.titleLabel.text = @"历史话题";
    }
    
    return header;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    
    if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
        ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = ColorWithRGB(0xF4F5F6);
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    
    if ([self.identity isEqualToString:@"2"]) {
        //游客
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"你还不是社团成员，快申请加入社团吧~~"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        
        TopicDetailViewController * detailVC = [TopicDetailViewController new];
        
        if (indexPath.section == 0) {
            
            if (self.todayArray.count > 0) {
                
                TopicModel * model = self.todayArray[indexPath.row];
                
                detailVC.model = model;
            }else{
                
                TopicModel * model = self.historyArray[indexPath.row];
                
                detailVC.model = model;
            }
            
        }else{
            
            TopicModel * model = self.historyArray[indexPath.row];
            
            detailVC.model = model;
        }
        
        [self.navigationController pushViewController:detailVC animated:YES];
    }
    
}


- (void)didSetUpButton{
    
    HiddenBar;
    
    PushTopicViewController * pushVC = [PushTopicViewController new];
    
    pushVC.group_id = self.group_id;
    
    [self.navigationController pushViewController:pushVC animated:YES];
}


#pragma mark - TableView 占位图
- (NSString *)xy_noDataViewMessage {
    
    return @"";
}

- (UIColor *)xy_noDataViewMessageColor {
    
    return [UIColor blackColor];
}

@end
